home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Fitts / ExpMenuView.m < prev    next >
Encoding:
Text File  |  1992-07-28  |  5.2 KB  |  260 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "ExpMenuView.h"
  5. #import <dpsclient/wraps.h>
  6. #import <appkit/color.h>
  7. #import "MenuItems.h"
  8. #import "ExpController.h"
  9.  
  10.  
  11. @implementation ExpMenuView
  12.  
  13. - initFrame: (NXRect *) frameRect
  14. {
  15.     state = 0;
  16.     
  17.     [super initFrame:frameRect];
  18.     
  19.     return self;
  20. }
  21.  
  22. - drawStartSquare
  23. {
  24.     NXRect rect;
  25.     
  26.     [self getFrame:&rect];   /* get view size */
  27.     
  28.     menu_rect( rect.size.width/2.0-10.0, rect.size.height/2.0+10.0, 20.0,
  29.                20.0);
  30.     PSgsave(); PSsetgray(0.5); PSfill(); PSgrestore();
  31.     PSsetgray(0.0); PSstroke();
  32.  
  33.     return self;
  34. }
  35.  
  36.  
  37. - drawSelf: (NXRect *) drawRects :(int) rectCount
  38. {
  39.     NXEraseRect(&bounds);
  40.     
  41.     if ( state == START_SQUARE )
  42.         [self drawStartSquare];
  43.     else if ( state == SHOW_MENU )
  44.         [super drawSelf:drawRects :rectCount];
  45.     
  46.     return self;
  47. }    
  48.  
  49. - hitTestVerticalMenu:(NXPoint) point
  50. {
  51.     int i;
  52.     float y;
  53.     float x_center;
  54.     float item_height, item_width, item_distance;
  55.     int hit_flag = 0;
  56.     NXRect rect;
  57.     
  58.     [self convertPoint:&point fromView:NULL];
  59.     
  60.     /* converts item millimeters to PS coord values */
  61.     item_width = MM_CONVERT * option1;
  62.     item_height  = MM_CONVERT * option2;
  63.     item_distance = MM_CONVERT * option3;
  64.  
  65.     [self getFrame:&rect];   /* get view size */
  66.     x_center = (rect.size.width / 2.0) - (item_width / 2.0);
  67.     
  68.     y = rect.size.height/2.0 + (numItems *(item_distance+item_height)) / 2.0;
  69.     
  70.     for ( i = 0; i < numItems; i++, y -= (item_distance+item_height) )
  71.     {
  72.     menu_rect(x_center, y, item_height, item_width);
  73.     
  74.     PSinfill(point.x, point.y, &hit_flag);
  75.     
  76.     if ( hit_flag )
  77.     {
  78.         fprintf(stderr, "Hit menu item %d!\n", i);
  79.         [expController menuItemHit:i];
  80.         break;
  81.     }
  82.  
  83.     }
  84.     
  85.     if ( !hit_flag )
  86.         [expController menuItemMissed];        
  87.     
  88.     return self;
  89. }
  90.  
  91. - hitTestCirclePieMenu:(NXPoint) point
  92. {
  93.     int i;
  94.     float x_center, y_center;
  95.     NXRect rect;
  96.     int hit_flag = 0;
  97.     float angle = 360 / (float) numItems;
  98.  
  99.     [self convertPoint:&point fromView:NULL];
  100.     
  101.     [self getFrame:&rect];   /* get view size */
  102.     x_center = rect.size.width / 2.0;
  103.     y_center = rect.size.height / 2.0;
  104.     
  105.     for ( i = 0, angle = 0.0; i < numItems;
  106.           i++, angle += 360/(float) numItems )
  107.     {
  108.         menu_pie(x_center, y_center, angle, angle+option1,
  109.              MM_CONVERT * option2, MM_CONVERT*(option2+option3));
  110.     
  111.     PSinfill(point.x, point.y, &hit_flag);
  112.     
  113.     if ( hit_flag )
  114.     {
  115.         fprintf(stderr, "Hit menu item %d\n", i);
  116.         [expController menuItemHit:i];
  117.         break;
  118.     }     
  119.     }
  120.     
  121.     if ( !hit_flag )
  122.     {
  123.         fprintf(stderr, "No item hit, x:%f, y:%f\n", point.x, point.y);
  124.     [expController menuItemMissed];
  125.     }
  126.     
  127.     return self;
  128. }
  129.  
  130. - hitTestCircleRectMenu:(NXPoint) point
  131. {
  132.     int i;
  133.     NXRect rect;
  134.     float x_center, y_center;
  135.     int hit_flag = 0;
  136.     float angle = 360 / (float) numItems;
  137.     
  138.     [self convertPoint:&point fromView:NULL];
  139.     
  140.     [self getFrame:&rect];   /* get view size */
  141.     x_center = rect.size.width / 2.0 - (MM_CONVERT * option1);
  142.     y_center = rect.size.height / 2.0;
  143.     
  144.     for ( i = 0, angle = 0.0; i < numItems;
  145.           i++, angle += 360/(float) numItems )
  146.     {
  147.         menu_pie_rect(x_center, y_center, MM_CONVERT*option3, angle,
  148.                    MM_CONVERT * option1, MM_CONVERT*option2);
  149.     
  150.     PSinfill(point.x, point.y, &hit_flag);
  151.  
  152.     if ( hit_flag )
  153.     {
  154.         fprintf(stderr, "Hit menu item %d\n", i);
  155.         [expController menuItemHit:i];
  156.         break;
  157.     }
  158.     }
  159.     
  160.     if ( !hit_flag )
  161.     {
  162.         fprintf(stderr, "No item hit, x:%f, y:%f\n", point.x, point.y);
  163.     [expController menuItemMissed];
  164.     }
  165.  
  166.     return self;
  167. }
  168.  
  169. - hitTestStartSquare:(NXPoint) point
  170. {
  171.     int hit_flag = 0;
  172.     NXRect rect;
  173.     
  174.     [self convertPoint:&point fromView:NULL];
  175.     [self getFrame:&rect];   /* get view size */
  176.  
  177.     menu_rect(rect.size.width/2.0-10.0, rect.size.height/2.0+10.0, 20.0, 20.0);
  178.  
  179.     PSinfill(point.x, point.y, &hit_flag);
  180.     
  181.     if ( hit_flag )
  182.     {
  183.         fprintf(stderr, "StartSquare hit.\n");
  184.     [expController startSquareHit];
  185.     
  186.     state = SHOW_MENU;
  187.     [self display];
  188.     }
  189.     else
  190.         fprintf(stderr, "StartSquare missed.\n");
  191.  
  192.     return self;
  193. }
  194.  
  195.  
  196. - mouseDown:(NXEvent *) event
  197. {
  198.     if ( event->type == NX_LMOUSEDOWN )
  199.         if ( state == START_SQUARE )
  200.         [self hitTestStartSquare:event->location];
  201.     
  202.     return self;
  203. }
  204.  
  205. - mouseUp:(NXEvent *) event
  206. {
  207.     if ( event->type == NX_LMOUSEUP )
  208.     {
  209.     if ( state == SHOW_MENU )
  210.     {
  211.         state = 0;
  212.         
  213.             switch ( menuType )
  214.         {
  215.             case VERTICAL:
  216.                 [self hitTestVerticalMenu:event->location];
  217.                     break;
  218.         
  219.         case CIRCLE_PIE:
  220.             [self hitTestCirclePieMenu:event->location];
  221.             break;
  222.             
  223.         case CIRCLE_RECTANGLE:
  224.             [self hitTestCircleRectMenu:event->location];
  225.             break;
  226.         }
  227.     }
  228.     }
  229.     
  230.     return self;
  231. }
  232.  
  233.  
  234. - setStartState
  235. {
  236. fprintf(stderr, "setStartState\n");
  237.  
  238.     state = START_SQUARE;
  239.     [self display];
  240.     
  241.     return self;
  242. }
  243.   
  244. - setMenuParms:(MVParms *) parms
  245. {
  246.     numItems = parms->num_items;
  247.     menuType = parms->menu_type;
  248.     option1  = parms->option1;
  249.     option2  = parms->option2;
  250.     option3  = parms->option3;
  251.     unselectedColour = parms->unselected_colour;
  252.     selectedColour   = parms->selected_colour;
  253.  
  254.     return self;
  255. }
  256.  
  257.  
  258.  
  259. @end
  260.